import axios from "axios";
import { ElMessage } from "element-plus";
const baseURL = '/api'
const instance = axios.create({ baseURL })

//添加相应拦截器
instance.interceptors.response.use(
    result => {
        //判断业务状态码
        console.log(result.data.code)
        if (result.data.code === 1) {
            return result.data;
        };
        //操作失败
        // alert(result.data.msg ? result.data.msg : '操作失败');
        ElMessage.error(result.data.message ? result.data.message : '操作失败')
        //异步操作的状态转化失败的状态
        return Promise.reject(result.data);
    },
    err => {
        alert('未登录')
        return Promise.reject(err);//异步的状态转化失败的状态
    }
)

instance.interceptors.request.use(
    function (config) {
        const token = localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = token
        }
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);


export default instance